package com.csu.letaobackend.controller;


import com.csu.letaobackend.bean.OrderItem;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.service.CartService;
import com.csu.letaobackend.vo.CartItemVO;
import com.csu.letaobackend.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@CrossOrigin
@RequestMapping("/cart")
public class CartController {

      @Autowired
      CartService cartService;

      @GetMapping("/cartitems")
      @ResponseBody
      public CommonResponse<List<CartItemVO>> getCartitemListByUsername(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return cartService.getCartitemListByUsername(login_user.getUsername());
      }


      @GetMapping("/cartitems/num")
      @ResponseBody
      public CommonResponse getCartitemNum(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            if(login_user==null)
                  return CommonResponse.createForError("用户未登录");
            return cartService.getCartitemNum(login_user.getUsername());
      }


      @PostMapping("/cartitems/{itemid}")
      @ResponseBody
      public CommonResponse<List<CartItemVO>> addCartitem(@PathVariable int itemid,
                                                          @RequestParam int num, @RequestParam double price,
                                                          @RequestParam String attrdescription, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            if (cartService.addCartitem(itemid, num, price, attrdescription, login_user.getUsername()).getStatus() == 0)
                  return cartService.getCartitemListByUsername(login_user.getUsername());
            else
                  return CommonResponse.createForError("商品库存不足");
      }

      @PutMapping("/cartitems/{itemid}")
      @ResponseBody
      public CommonResponse<List<CartItemVO>> updateCartitem(@PathVariable int itemid,
                                                             @RequestParam int num, @RequestParam double price,
                                                             @RequestParam String attrdescription, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            if (cartService.updateCartitem(itemid, num, price, attrdescription, login_user.getUsername()).getStatus() == 0)
                  return cartService.getCartitemListByUsername(login_user.getUsername());
            else
                  return CommonResponse.createForError("商品库存不足");
      }

      @DeleteMapping("/cartitems/{itemid}")
      @ResponseBody
      public CommonResponse<List<CartItemVO>> deleteCartitem(@PathVariable int itemid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            if (cartService.deleteCartitem(itemid, login_user.getUsername()).getStatus() == 0)
                  return cartService.getCartitemListByUsername(login_user.getUsername());
            else
                  return CommonResponse.createForError("删除失败");
      }

      @PostMapping("/cartitems/clear")
      @ResponseBody
      public CommonResponse<List<OrderVO>> clearCart(@RequestParam int receivingid, @RequestParam String[] couponids, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            List<Integer> list = new ArrayList<>();
            for(int i=0;i<couponids.length;i++){
                  list.add(Integer.valueOf(couponids[i]));
            }
            return cartService.createOrders(login_user.getUsername(), receivingid, list);
      }
}
